package empox.pos.system.print;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import empox.common.DBConst;
import empox.common.EmpoxConst;

public class PrintDB {
	static ArrayList<PrintElement> headerElementList = new ArrayList<PrintElement>();
	static ArrayList<PrintElement> cycleElementList = new ArrayList<PrintElement>();
	static ArrayList<PrintElement> bottomElementList = new ArrayList<PrintElement>();

	static ArrayList<CustomInfo> customElementList = new ArrayList<CustomInfo>();

	SQLiteDatabase dbprint;
	Cursor cursor;

	final String TAG = "ALTERTEXT";

	static List<PrintElement> tempElementlist = new ArrayList<PrintElement>();
	static List<CustomInfo> tempcustomlist = new ArrayList<CustomInfo>();

	public PrintDB() {
		customElementList.clear();

		headerElementList.clear();
		cycleElementList.clear();
		bottomElementList.clear();
	}

	public void getCustomInfo() {
		tempElementlist.clear();
		tempcustomlist.clear();

		try {
			// for (int i = 0; i < customElementList.size(); i++) {
			// CustomInfo ci = customElementList.get(i);
			//
			// if (ci.isValid == 1) {
			// PrintInfo pi = new PrintInfo();
			// pi.dispName = ci.dispName;
			//
			// printinfolist.add(pi);
			// }
			// }

			for (int i = 0; i < headerElementList.size(); i++) {
				PrintElement pe = headerElementList.get(i);
				String name = pe.name;
				Log.v("text", "name:"+name);
				if (name.startsWith(TAG) == true) {
					tempElementlist.add(pe);
					int index = getIndex(name.substring(TAG.length(),
							name.length()).charAt(0));
					
					Log.v("text","index:"+index);
					
					CustomInfo ci = new CustomInfo();
					ci.dispName = customElementList.get(index).dispName;
					tempcustomlist.add(ci);
				}
			}

			for (int i = 0; i < cycleElementList.size(); i++) {
				PrintElement pe = cycleElementList.get(i);
				String name = pe.name;
				if (name.startsWith(TAG) == true) {
					tempElementlist.add(pe);
					int index = getIndex(name.substring(TAG.length(),
							name.length()).charAt(0));

					CustomInfo ci = new CustomInfo();
					ci.dispName = customElementList.get(index).dispName;
					tempcustomlist.add(ci);
				}
			}
			
			for(int i = 0; i<bottomElementList.size(); i++)
			{
				PrintElement pe = bottomElementList.get(i);
				String name = pe.name;
				if(name.startsWith(TAG) == true)
				{
					tempElementlist.add(pe);
					int index = getIndex(name.substring(TAG.length(),
							name.length()).charAt(0));

					CustomInfo ci = new CustomInfo();
					ci.dispName = customElementList.get(index).dispName;
					tempcustomlist.add(ci);
				}
			}
			
			

		} catch (Exception e) {
			e.printStackTrace();

			tempElementlist.clear();
			tempcustomlist.clear();
		}

	}

	public int getIndex(char index)// added by xushuang 20090205
	{
		int trueIndex;
		switch (index) {
		case '0':
			trueIndex = 0;
			break;
		case '1':
			trueIndex = 1;
			break;
		case '2':
			trueIndex = 2;
			break;
		case '3':
			trueIndex = 3;
			break;
		case '4':
			trueIndex = 4;
			break;
		case '5':
			trueIndex = 5;
			break;
		case '6':
			trueIndex = 6;
			break;
		case '7':
			trueIndex = 7;
			break;
		case '8':
			trueIndex = 8;
			break;
		case '9':
			trueIndex = 9;
			break;
		case 'A':
		case 'a':
			trueIndex = 10;
			break;
		case 'B':
		case 'b':
			trueIndex = 11;
			break;
		case 'C':
		case 'c':
			trueIndex = 12;
			break;
		case 'D':
		case 'd':
			trueIndex = 13;
			break;
		case 'E':
		case 'e':
			trueIndex = 14;
			break;
		case 'F':
		case 'f':
			trueIndex = 15;
			break;
		default:
			trueIndex = 0;
			break;
		}
		return trueIndex;
	}

	public void loadPrintTemplet() {
		try {
			dbprint = SQLiteDatabase.openOrCreateDatabase(
					EmpoxConst.$DATABASEPATH + DBConst.$dbSet, null);

			String sql = "select * from printtemphead";
			cursor = dbprint.rawQuery(sql, null);
			
			while (cursor.moveToNext()) {
				PrintElement pe = new PrintElement();

				pe.name = cursor.getString(cursor.getColumnIndex("name"));
				pe.content = cursor.getString(cursor.getColumnIndex("content"));
				
				pe.setKind(cursor.getString(cursor.getColumnIndex("kind")));
				pe.setWidth(cursor.getString(cursor.getColumnIndex("width")));
				pe.setAlign(cursor.getString(cursor.getColumnIndex("align")));
				pe.setDigit(cursor.getString(cursor.getColumnIndex("digit")));
				pe.setOrder(cursor.getString(cursor.getColumnIndex("orders")));
				pe.setChars(cursor.getString(cursor.getColumnIndex("chars")));
				pe.setPreBR(cursor.getString(cursor.getColumnIndex("preBR")));
				pe.setAppendBR(cursor.getString(cursor
						.getColumnIndex("appendBR")));

				headerElementList.add(pe);
			}
			cursor.close();

			sql = "select * from printtempbody";
			cursor = dbprint.rawQuery(sql, null);

			while (cursor.moveToNext()) {
				PrintElement pe = new PrintElement();
				
				Log.v("text", "name:"+cursor.getColumnIndex("name"));
				
				pe.name = cursor.getString(cursor.getColumnIndex("name"));
				
				
				
				pe.content = cursor.getString(cursor.getColumnIndex("content"));

				pe.setKind(cursor.getString(cursor.getColumnIndex("kind")));
				pe.setWidth(cursor.getString(cursor.getColumnIndex("width")));
				pe.setAlign(cursor.getString(cursor.getColumnIndex("align")));
				pe.setDigit(cursor.getString(cursor.getColumnIndex("digit")));
				pe.setOrder(cursor.getString(cursor.getColumnIndex("orders")));
				pe.setChars(cursor.getString(cursor.getColumnIndex("chars")));
				pe.setPreBR(cursor.getString(cursor.getColumnIndex("preBR")));
				pe.setAppendBR(cursor.getString(cursor
						.getColumnIndex("appendBR")));

				cycleElementList.add(pe);
			}
			cursor.close();

			sql = "select * from printtempbottom";
			cursor = dbprint.rawQuery(sql, null);

			while (cursor.moveToNext()) {
				PrintElement pe = new PrintElement();

				pe.name = cursor.getString(cursor.getColumnIndex("name"));
				pe.content = cursor.getString(cursor.getColumnIndex("content"));

				pe.setKind(cursor.getString(cursor.getColumnIndex("kind")));
				pe.setWidth(cursor.getString(cursor.getColumnIndex("width")));
				pe.setAlign(cursor.getString(cursor.getColumnIndex("align")));
				pe.setDigit(cursor.getString(cursor.getColumnIndex("digit")));
				pe.setOrder(cursor.getString(cursor.getColumnIndex("orders")));
				pe.setChars(cursor.getString(cursor.getColumnIndex("chars")));
				pe.setPreBR(cursor.getString(cursor.getColumnIndex("preBR")));
				pe.setAppendBR(cursor.getString(cursor
						.getColumnIndex("appendBR")));

				bottomElementList.add(pe);
			}
			cursor.close();
			
			sql = "select * from printmsghead";
			cursor = dbprint.rawQuery(sql, null);

			while (cursor.moveToNext()) {
				CustomInfo ci=new CustomInfo();
				
				ci.dispName = cursor.getString(cursor.getColumnIndex("dispName"));
				ci.setIsValid(cursor.getString(cursor.getColumnIndex("isValid")));
				customElementList.add(ci);
				
			}
			
			cursor.close();
			cursor = null;

			dbprint.close();
		} catch (Exception e) {
			e.printStackTrace();

			headerElementList.clear();
			cycleElementList.clear();
			bottomElementList.clear();
		} finally {
			if (cursor != null)
				cursor.close();
			cursor = null;

			if (dbprint != null)
				dbprint.close();
			dbprint = null;
		}

	}
}
